package com.itheima.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.constant.RedisConstant;
import com.itheima.dao.OrderDao;
import com.itheima.domain.*;
import com.itheima.service.OrderService;
import org.springframework.beans.factory.annotation.Autowired;
import redis.clients.jedis.JedisPool;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 创建时间：2020-08-22-9:41 xiaotian(●'◡'●))
 * 类说明：
 * 类注解：
 */

@Service
public class OrderServiceImpl implements OrderService {

    @Autowired
    private OrderDao orderDao;
    @Autowired
    private JedisPool jedisPool;


    /*-----------------------------------setmeal.html----------------------------------------*/
    @Override
    public List<CheckGroup> findCheckGroup() {
        return orderDao.findCheckGroup();
    }

    @Override
    public PageResult findSetMealPage(Integer currentPage, Integer pageSize, String queryString) {
        PageHelper.startPage(currentPage,pageSize);  //查询setmeal表
        Page page = orderDao.findSetMealPage(queryString);
        return new PageResult(page.getTotal(),page.getResult());
    }

    @Override
    public void deleteSetmeal(Integer id) {  //删除
        orderDao.deleteSetmeal(id);
    }

    @Override
    public List<CheckGroup> findCheckItemPage2() {  //简单查询检查组
        List<CheckGroup> list = orderDao.findCheckItemPage2();
        return list;
    }

    //没有缓存图片功能，下面是有缓存的
/*    @Override
    public void addSetmeal(Setmeal setmeal, Integer[] checkgroupIds) {  //保存数据
        orderDao.addSetmeal(setmeal);
        if(checkgroupIds != null && checkgroupIds.length > 0){
//绑定套餐和检查组的多对多关系
            setSetmealAndCheckGroup(setmeal.getId(),checkgroupIds);
        }
    }*/
    //新增套餐
    public void addSetmeal(Setmeal setmeal, Integer[] checkgroupIds) {
        orderDao.addSetmeal(setmeal);
        if(checkgroupIds != null && checkgroupIds.length > 0){
            setSetmealAndCheckGroup(setmeal.getId(),checkgroupIds);
        }//将图片名称保存到Redis
        savePic2Redis(setmeal.getImg());
    }//将图片名称保存到Redis
    private void savePic2Redis(String pic){
        jedisPool.getResource().sadd(RedisConstant.SETMEAL_PIC_DB_RESOURCES,pic);
    }

    //绑定套餐和检查组的多对多关系
    private void setSetmealAndCheckGroup(Integer id, Integer[] checkgroupIds) {
        for (Integer checkgroupId : checkgroupIds) {
            Map<String,Integer> map = new HashMap<>();
            map.put("setmeal_id",id);
            map.put("checkgroup_id",checkgroupId);
            orderDao.setSetmealAndCheckGroup(map);
        }
    }


    /*-----------------------------------CheckItem.html----------------------------------------*/
    @Override
    public PageResult findCheckItem(Integer currentPage, Integer pageSize, String queryString) {
        PageHelper.startPage(currentPage,pageSize);
        Page page = orderDao.findCheckItem(queryString);
        return new PageResult(page.getTotal(),page.getResult());
    }

    @Override
    public void checkitemDelete(Integer id) {
        orderDao.checkitemDelete(id);
    }

    @Override
    public void addCheckitem(OrderCheckItem orderCheckItem) {
        orderDao.addCheckitem(orderCheckItem);
    }

    @Override
    public void checkitemedit(OrderCheckItem orderCheckItem) {
        orderDao.checkitemedit(orderCheckItem);
    }




    @Override
    public List<Map> getOrderSettingByMonth(String date) { //2019-3
        String dateBegin = date + "-1";
        String dateEnd = date + "-31";
        HashMap map = new HashMap();
        map.put("dateBegin",dateBegin);
        map.put("dateEnd",dateEnd);

        List<OrderSetting> list = orderDao.getOrderSettingByMonth(map);  //得到31天的每个t_orderSetting表的数据
        ArrayList<Map> data = new ArrayList<>();
        for (OrderSetting orderSetting : list) {  //进行赋值data集合 返回一个map键值对的集合
            Map orderSettingMap = new HashMap();
            orderSettingMap.put("date",orderSetting.getOrderDate().getDate());//获得日期号

            orderSettingMap.put("number",orderSetting.getNumber());//可预约人数
            orderSettingMap.put("reservations",orderSetting.getReservations());//已预约人数
            data.add(orderSettingMap);
        }
        return data;  //第一个数据：map[date,number,reservations]
    }


    @Override
    //根据日期修改可预约人数
    public void editNumberByDate(OrderSetting orderSetting) {
        long count = orderDao.findCountByOrderDate(orderSetting.getOrderDate());
        if (count > 0) {
            orderDao.editNumberByOrderDate(orderSetting);  //当前日期已经进行了预约设置，需要进行修改操作
        } else {
            orderDao.addNumberByOrderDate(orderSetting);  //当前日期没有进行预约设置，进行添加操作
        }
    }

    @Override
    public void addUpload(List<OrderSetting> list) {
        if(list != null && list.size() > 0){
            for (OrderSetting orderSetting : list) {  //检查此数据（日期）是否存在
                long count = orderDao.findCountByOrderDate(orderSetting.getOrderDate());
                if(count > 0){  //已经存在，执行更新操作
                    orderDao.editNumberByOrderDate(orderSetting);
                }else{  //不存在，执行添加操作
                    orderDao.addNumberByOrderDate(orderSetting);
                }
            }
        }
    }



    /*-----------------------------------order.html----------------------------------------*/
    public PageResult pageQuery(Integer currentPage, Integer pageSize, String queryString) {
        PageHelper.startPage(currentPage,pageSize);
        Page<Order> page = orderDao.selectByCondition(queryString);
        return new PageResult(page.getTotal(),page.getResult());
    }

    @Override
    public List<Order> findOrderAll() {
        return null;
    }

    @Override
    public List<OrderSetting> findOrderSettingAll() {
        return null;
    }

    @Override
    public void deleteOrder(int id) {
        orderDao.deleteOrder(id);
    }


    //修改order一条数据
    @Override
    public void orderUpdate(Order order) {
        orderDao.orderUpdate(order);
    }

    @Override
    public Order findOrder(Integer id) {
        return orderDao.findOrder(id);
    }

    @Override
    public void orderAdd(Order order) {
        orderDao.orderAdd(order);
    }




    /*-----------------------------------OrderCheckItem.html----------------------------------------*/
    @Override
    public List<OrderCheckItem> findCheckItemAll() {  //查询检查项数据
        return orderDao.findCheckItemAll();
    }

    //检查组的条件 分页查询
    @Override
    public PageResult findCheckItemPage(Integer currentPage, Integer pageSize, String queryString) {
        PageHelper.startPage(currentPage,pageSize);
        Page<CheckGroup> page = orderDao.findCheckItemPage(queryString);
        return new PageResult(page.getTotal(),page.getResult());
    }

    //添加检查组合，同时需要设置检查组合和检查项的关联关系
    public void addcheckitem(CheckGroup checkGroup, Integer[] checkitemIds) {
        orderDao.addcheckitem(checkGroup);
        setCheckGroupAndCheckItem(checkGroup.getId(),checkitemIds);
    }

    //设置检查组合和检查项的关联关系
    public void setCheckGroupAndCheckItem(Integer checkGroupId,Integer[] checkitemIds){
        if(checkitemIds != null && checkitemIds.length > 0){
            for (Integer checkitemId : checkitemIds) {
                Map<String,Integer> map = new HashMap<>();
                map.put("checkgroup_id",checkGroupId);
                map.put("checkitem_id",checkitemId);
                orderDao.setCheckGroupAndCheckItem(map);
            }
        }
    }


    //修改：根据id查询回显当前行数据
    @Override
    public CheckGroup findById(Integer id) {
        return orderDao.findById(id);
    }

    //
    @Override
    public List<Integer> findCheckItemIdsByCheckGroupId(Integer id) {
        return orderDao.findCheckItemIdsByCheckGroupId(id);

    }

    @Override
    public void edit(CheckGroup checkGroup, Integer[] checkitemIds) {
        //根据检查组id删除中间表数据（清理原有关联关系）
        orderDao.deleteAssociation(checkGroup.getId());
        //向中间表(t_checkgroup_checkitem)插入数据（建立检查组和检查项关联关系）
        setCheckGroupAndCheckItem(checkGroup.getId(),checkitemIds);
        //更新检查组基本信息
        orderDao.edit(checkGroup);
    }

    //删除CheckGroup的一条数据
    @Override
    public void deleteCheckGroup(int id) {
        orderDao.deleteAssociation(id);  //消除checkgroup关联的checkitem关系
        orderDao.deleteCheckGroup(id);

    }



}
